package cn.chia.utils;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;

import cn.chia.back.action.UserAction;
import cn.chia.pay.ali.util.ConfKit;
import cn.chia.pay.wechat.util.common.HttpHandler;
import cn.chia.pojo.User;

public class GetUserFromWeixin {
	
	private static Logger logger = Logger.getLogger(GetUserFromWeixin.class);
	//使用access_token获取用户信息OPENID
	public static String returnUserInfo(String code){
		
		String openId = null;		
		try{
		User user=new User();
		logger.info("in returnUserInfo------ and code is " + code);
		String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
		url=url.replace("APPID", ConfKit.get("AppId"));
		url=url.replace("SECRET", ConfKit.get("appSecret"));
		url=url.replace("CODE", code);
		//String json="";
		String str = HttpHandler.sendGetRequest(url).toJSONString();
		logger.info("\n ++++++++++++++++++++++ http result" + str + "++++++++++++++++++++");
		JSON.parseObject(str).get("openid");

		
		System.out.println(JSON.parseObject(str).get("openid").toString());
		
		user.setWid(JSON.parseObject(str).get("openid").toString());
		
		openId = user.getWid();
		
		}catch(Exception e){
			e.printStackTrace();
		}

		return openId;
	}
	
	/*
	 * 2014-07-02
	 * 使用openid获得用户的信息
	 
	// 使用openid获得用户的信息
			public static User getweixinuser(String openid) {
				
				String openId = null;
				
				User user = new User();
				try {
					String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
					url = url.replace("OPENID",openid);
					
					String str = HttpHandler.sendPostRequest(url, null);
					
					user.setWid(JSON.parseObject(str).get("openid").toString());
					
					//将用户所有的信息放入实体中
				} catch (Exception e) {
					openId = e.toString();
				}
				return user;
			}
			*/
		/*	public static String getweixinuser1(String openid) {
				String openId = null;
				List<User> ulist=new ArrayList<User>();
				User user = new User();
				try {
					String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
					url = url.replace("OPENID",openid);
					String json = "";
					JSONObject jsonObject = WeixinUtil.httpRequest(url, "POST", json);
					// UserInfo=JSONObject.fromObject(jsonObject).toString()+":::"+url;//调试用
					//将用户所有的信息放入实体中
					
					openId = jsonObject.toString();

				} catch (Exception e) {
					openId = e.toString();
				}
				return openId;
			}*/
}
